package com.jp.fm.dao;

import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.jp.fm.EasyMockParent;
import com.jp.fm.TestHelper;
import com.jp.fm.data.Budget;
import com.jp.fm.data.Category;
import com.jp.fm.data.Transaction;

public class BudgetDaoTest extends EasyMockParent {

	private BudgetDao budgetDao = null;

	private CategoryDao mockCategoryDao = null;
	private TransactionDao mockTransactionDao = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		TestHelper.setupTestDb();
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {

		super.setUp();

		budgetDao = new BudgetDao();
	}

	@After
	public void tearDown() throws Exception {

		super.tearDown();
	}

	@Test
	public final void testFindByDate() {

		List<Transaction> list = new ArrayList<Transaction>();
		list.add(new Transaction(null, null, null, Float.valueOf("10.00"),
				null, null, null, false, null));

		expect(
				mockTransactionDao.findByAny(anyObject(Category.class),
						anyObject(Date.class), anyObject(Date.class),
						anyObject(String.class), anyObject(String.class),
						anyObject(Boolean.class))).andReturn(list).times(2);

		replayMocks();

		Calendar c = new GregorianCalendar();
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.YEAR, 2013);

		budgetDao.setTransactionDao(mockTransactionDao);
		List<Budget> ret = budgetDao.findByDate(c.getTime());

		assertNotNull(ret);
		assertEquals(2, ret.size());
	}

	@Test
	public final void testFindAll() {

		replayMocks();

		List<Budget> ret = budgetDao.findAll();

		assertNotNull(ret);
		assertEquals(2, ret.size());
	}

	@Test
	public final void testWriteObject() {

		replayMocks();

		Budget budget = new Budget(null, new Date(), Float.valueOf("10.99"),
				"Monthly", "test1", new Category(5, "testCategory"));

		Integer ret = budgetDao.writeObject(budget);

		assertNotNull(ret);
	}

	@Test
	public final void testDelete() {

		replayMocks();

		Budget budget = new Budget(null, new Date(), Float.valueOf("10.99"),
				"Monthly", "test1", new Category(5, "testCategory"));

		Integer ret = budgetDao.writeObject(budget);
		assertNotNull(ret);
		budget.setId(ret);
		budgetDao.delete(budget);
	}

	@Test
	public final void testCopyBudgetsFromLastMonth() {

		replayMocks();

		budgetDao.copyBudgetsFromLastMonth();
	}

}
